﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class TourController : Controller
    {

        //注入
        private ITourService _tourService;
        private IProvinceLocationService _provinceLocationService;
        private ICityLocationService _cityLocationService;
        public TourController(ITourService tourService, IProvinceLocationService provinceLocationService, ICityLocationService cityLocationService)
        {
            _tourService = tourService;
            _provinceLocationService = provinceLocationService;
            _cityLocationService = cityLocationService;
        }

        /// <summary>
        /// 获取团购列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <returns></returns>
        public JsonResult GetTourList(int page, int limit)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Tour, bool>> lambdaCategory = a => true;
            Expression<Func<Tour, int>> orderCategory = a => a.TourId;
            //创建List集合接收业务层返回值
            List<Tour> tours = _tourService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //赋值
            responseResult.code = 0;
            responseResult.msg = string.Empty;
            responseResult.data = tours;
            responseResult.count = count;
            return Json(responseResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除团购
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult DelTour(Tour tour)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _tourService.Delete(tour);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 根据父级id获取省份集合
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult GetProvince(Countrylocation countrylocation)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //出发地--根据父级id获取对应省份对象
            Expression<Func<Provincelocation, bool>> expression = a => a.ParentId == countrylocation.Id; 
            responseResult.data = _provinceLocationService.QueryList(expression);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 根据父级id获取城市集合
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult GetCity(Provincelocation provincelocation)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //出发地--根据父级id获取对应省份对象
            Expression<Func<Citylocation, bool>> expression = a => a.ParentId == provincelocation.Id;
            responseResult.data = _cityLocationService.QueryList(expression);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        public JsonResult EditTour(Tour tour)
        { 
            //创建返回结果对象
            ResponseResult responseResult = _tourService.EditTour(tour);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }
    }
}